package com.body.park.common.response;

import com.body.park.common.constants.CommonConstants;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Author chaoxian.wu
 * @DATE 2022/5/2 0:08
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResponseEntity<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private int code;

    private String msg;

    private T data;

    public static <T> ResponseEntity<T> ok() {
        return restResult(null, CommonConstants.SUCCESS, null);
    }

    public static <T> ResponseEntity<T> ok(T data) {
        return restResult(data, CommonConstants.SUCCESS, null);
    }

    public static <T> ResponseEntity<T> ok(T data, String msg) {
        return restResult(data, CommonConstants.SUCCESS, msg);
    }

    public static <T> ResponseEntity<T> failed() {
        return restResult(null, CommonConstants.FAIL, null);
    }

    public static <T> ResponseEntity<T> failed(String msg) {
        return restResult(null, CommonConstants.FAIL, msg);
    }

    public static <T> ResponseEntity<T> failed(T data) {
        return restResult(data, CommonConstants.FAIL, null);
    }

    public static <T> ResponseEntity<T> failed(T data, String msg) {
        return restResult(data, CommonConstants.FAIL, msg);
    }

    private static <T> ResponseEntity<T> restResult(T data, int code, String msg) {
        ResponseEntity<T> apiResult = new ResponseEntity<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public boolean isSuccess() {
        return this.code == CommonConstants.SUCCESS;
    }
}
